package cn.tradewin.advisor.persist.services.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;

import cn.tradewin.advisor.persist.mapper.PassetsMasterMapper;
import cn.tradewin.advisor.persist.model.ChkCategory;
import cn.tradewin.advisor.persist.model.ChkLang;
import cn.tradewin.advisor.persist.model.CodeLabel;
import cn.tradewin.advisor.persist.services.PassetsMasterService;

public class PassetsMasterServiceImpl implements PassetsMasterService {
	
	private final SqlSessionFactory sqlSessionFactory;
	
	public PassetsMasterServiceImpl(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	@Override
	public List<ChkCategory> getChkCategories(Integer lang) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			Map<String, Integer> param = CollectionFactory.newMap();
			param.put("lang", lang);
			return mapper.getChkCategories(param);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addChkCategory(ChkCategory category) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			int count = mapper.addChkCategory(category);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delChkCategory(Integer categoryCode) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			int count = mapper.delChkCategory(categoryCode);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<ChkLang> getChkLangs() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			return mapper.getChkLangs();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int addChkLang(ChkLang lang) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			int count = mapper.addChkLang(lang);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public int delChkLang(Integer code) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			int count = mapper.delChkLang(code);
			session.commit();
			return count;
		} catch (Exception e) {
			if (session != null) {
				session.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getLangOptions() {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			return mapper.getLangOptions();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public List<CodeLabel> getCategoryOptions(Integer lang) {
		SqlSession session = null;
		try {
			session = sqlSessionFactory.openSession();
			PassetsMasterMapper mapper = session.getMapper(PassetsMasterMapper.class);
			Map<String, Integer> param = CollectionFactory.newMap();
			param.put("lang", lang);
			return mapper.getCategoryOptions(param);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
